package org.jeecg.modules.simulator.model.train.manager;

import org.jeecg.modules.simulator.model.orbit.OrbitPath;
import org.jeecg.modules.simulator.model.train.action.TrainActionRecorder;
import org.jeecg.modules.simulator.model.train.action.TrainActionRecorders;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalTime;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: gp
 * @description:
 * @author: lydms
 * @create: 2024-08-08 10:00
 **/
public class TrainActionRdsManager {

    /**
     * @description:动作记录集合，用来记录所有车的动作记录， 初始化可以根据列车时刻表，也可以自定义
     * Key:trainId+"-"+trainNum
     * @author: renzhg
     * @date: 2024/7/29 9:46
     **/
    @Setter
    @Getter
    Map<String, TrainActionRecorders> trainActionRecordersMap = new LinkedHashMap<>();

    Map<String, TrainActionRecorders> trainActionRecordersSecondMap= new LinkedHashMap<>();





    public void setTrainActionRecordersSecondMap(Map<String, TrainActionRecorders> trainActionRecordersSecondMap, OrbitPath upOrbitPath) {

        for(Map.Entry<String,TrainActionRecorders> entry:trainActionRecordersSecondMap.entrySet()){
            TrainActionRecorders trainActionRecorders = entry.getValue();

            TrainActionRecorder fristTrainActionRecorder = trainActionRecorders.getFristTrainActionRecorder();
            TrainActionRecorder trainActionSecondRecorder = trainActionRecorders.getTrainActionRecordMap().get(2);

            if(fristTrainActionRecorder!=null && trainActionSecondRecorder!=null){
                if(trainActionSecondRecorder.getAffiliateType().equals("sect")){


                    String tableItemName = trainActionSecondRecorder.getTableItemName();




                }
            }
            Map<Integer, TrainActionRecorder> trainActionRecordMap = trainActionRecorders.getTrainActionRecordMap();

//            trainActionRecordMap

        }





    }

    Map<String, LocalTime> firstTimeMap = new LinkedHashMap<>();


    /**
     * @description: 给定车辆id和车次号，得到这辆车的完成的动作
     * @author: renzhg
     * @date: 2024/8/12 14:41
     **/
    public Map<Integer, TrainActionRecorder> getCertainTrainActionOffset(String trainidnum) {
        TrainActionRecorders trainActionRecorders = trainActionRecordersMap.get(trainidnum);
        Map<Integer, TrainActionRecorder> trainActionRecordMap = trainActionRecorders.getTrainActionRecordMap();
        Map<Integer, TrainActionRecorder> activeRecords = trainActionRecordMap.entrySet().stream()
                .filter(entry -> entry.getValue().isFinished()) // 过滤 isActive 为 true 的条目
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        return activeRecords;

    }

    //指定时间加车---从线路头部添加车辆


    //暂时想的一个限制条件是：当前加车时间应该比现在时间晚8分钟

    public void addTrainByOneTime(String now,String addTimePt){
        LocalTime nowTime = LocalTime.parse(now);
        LocalTime addTime = LocalTime.parse(addTimePt);
        if (nowTime.isBefore(addTime.minusMinutes(8))) {
            // nowTime is at least 8 minutes before addTime
        }
//        for (Map.Entry<String, TrainActionRecorders> entry : trainActionRecordersMap.entrySet()) {
//            String key = entry.getKey();
//            TrainActionRecorders value = entry.getValue();
//            LocalTime firstTime = firstTimeMap.get(key);
//            if (nowTime.isAfter(firstTime) && nowTime.isBefore(firstTime.plusSeconds(10))) {
//                if (addTime.isAfter(firstTime) && addTime.isBefore(firstTime.plusSeconds(10))) {
//                    TrainActionRecorders trainActionRecorders = new TrainActionRecorders();
//                    trainActionRecorders.setTrainIdNum(key);
//                    trainActionRecorders.setTrainActionRecordMap(value.getTrainActionRecordMap());
//                    trainActionRecordersMap.put(key, trainActionRecorders);
//                }
//            }
//        }
    }



    //指定地点和时间加车







}
